package cn.iocoder.yudao.module.pay.dal.mysql.packageorder;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.pay.controller.app.packageorder.vo.AppPackageOrderPageReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.packageorder.PackageOrderDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.pay.controller.admin.packageorder.vo.*;
import org.apache.ibatis.annotations.Param;

/**
 * 套餐订单 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface PackageOrderMapper extends BaseMapperX<PackageOrderDO> {

    default PageResult<PackageOrderDO> selectPage(PackageOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PackageOrderDO>()
                .eqIfPresent(PackageOrderDO::getUserId, reqVO.getUserId())
                .eqIfPresent(PackageOrderDO::getPackageId, reqVO.getPackageId())
                .likeIfPresent(PackageOrderDO::getPackageName, reqVO.getPackageName())
                .eqIfPresent(PackageOrderDO::getPrice, reqVO.getPrice())
                .eqIfPresent(PackageOrderDO::getPayStatus, reqVO.getPayStatus())
                .eqIfPresent(PackageOrderDO::getPayOrderId, reqVO.getPayOrderId())
                .eqIfPresent(PackageOrderDO::getPayChannelCode, reqVO.getPayChannelCode())
                .betweenIfPresent(PackageOrderDO::getPayTime, reqVO.getPayTime())
                .eqIfPresent(PackageOrderDO::getPayRefundId, reqVO.getPayRefundId())
                .eqIfPresent(PackageOrderDO::getRefundPrice, reqVO.getRefundPrice())
                .betweenIfPresent(PackageOrderDO::getRefundTime, reqVO.getRefundTime())
                .betweenIfPresent(PackageOrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(PackageOrderDO::getId));
    }

    default PageResult<PackageOrderDO> selectPageForApp(AppPackageOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PackageOrderDO>()
                .eqIfPresent(PackageOrderDO::getUserId, reqVO.getUserId())
                .eqIfPresent(PackageOrderDO::getPackageId, reqVO.getPackageId())
                .likeIfPresent(PackageOrderDO::getPackageName, reqVO.getPackageName())
                .eqIfPresent(PackageOrderDO::getPrice, reqVO.getPrice())
                .eqIfPresent(PackageOrderDO::getPayStatus, reqVO.getPayStatus())
                .eqIfPresent(PackageOrderDO::getPayOrderId, reqVO.getPayOrderId())
                .eqIfPresent(PackageOrderDO::getPayChannelCode, reqVO.getPayChannelCode())
                .betweenIfPresent(PackageOrderDO::getPayTime, reqVO.getPayTime())
                .eqIfPresent(PackageOrderDO::getPayRefundId, reqVO.getPayRefundId())
                .eqIfPresent(PackageOrderDO::getRefundPrice, reqVO.getRefundPrice())
                .betweenIfPresent(PackageOrderDO::getRefundTime, reqVO.getRefundTime())
                .betweenIfPresent(PackageOrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(PackageOrderDO::getId));
    }

    default int updateByIdAndPayed(Long id, boolean wherePayed, PackageOrderDO updateObj) {
        return update(updateObj, new LambdaQueryWrapperX<PackageOrderDO>()
                .eq(PackageOrderDO::getId, id).eq(PackageOrderDO::getPayStatus, wherePayed));
    }

    Integer totalPrice(@Param("userId") Long userId);

}